﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Common;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Adminer
{
    public partial class Admin : AdminPage
    {
        private Adminer curAdminer = new Adminer();
        private GroupAdmin curGroupAdmin = new GroupAdmin();
        private string type = "";
        private string id = "";
        private string power = "";
        private int pageSize = 15;
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            power = Request.QueryString["power"] == null ? "" : Request.QueryString["power"].Trim();
            if (!IsPostBack)
            {
                type = (Request.QueryString["t"] == null) ? "" : Request.QueryString["t"].Trim();
                id = (Request.QueryString["id"] == null) ? "" : Request.QueryString["id"].Trim();
                if (type == "delete" && id != "")
                {
                    int reid=0;
                    int.TryParse(id, out reid);
                    Delete_user(reid);
                }
                else
                {
                    try
                    {
                        DdlMenu_bind();
                        Repeater_admin_bind();
                    }
                    catch (Exception ex) { Response.Redirect("../Message.aspx?message=数据绑定错误！" + ex.Message + "&z=no"); }
                }
            }
        }

        //删除一条数据
        void Delete_user(int uid)
        {
            string tpower = curAdminer.GetPower(uid).Trim();
            if (tpower != "0")
            {
                curAdminer.Delete(uid);
                Response.Redirect("Admin.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=您不能删除超级管理员！&z=no");
            }
        }

        #region 类别数据绑定
        void DdlMenu_bind()
        {
            DataTable dt = curGroupAdmin.GetPowerList();
            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("所有分类", ""));
            int tCount = 1;
            foreach (DataRow dr in dt.Rows)
            {
                string tpower = dr["power"].ToString();
                string tname = dr["name"].ToString();
                ListItem li = new ListItem(tname, tpower);
                DdlMenu.Items.Add(li);
                if (power == tpower)
                {
                    DdlMenu.SelectedIndex = tCount;
                }
                tCount++;
            }
        }
        #endregion

        #region 数据绑定
        void Repeater_admin_bind()
        {
            string gopage = (Request.QueryString["page"] == null) ? "0" : Request.QueryString["page"].Trim();
            int page = 0;
            int.TryParse(gopage, out page);

            int recordCount = 0;
            DataTable dt = curAdminer.GetList(pageSize, (page - 1), power, out recordCount);
            litPager.Text = PlugNT.Common.Pager.Pagination(recordCount, pageSize, page, "Admin.aspx", GetBindUrl(), "", true, true);

            Repeater_admin.DataSource = dt;
            Repeater_admin.DataBind();
        }
        #endregion

        #region 显示数据处理
        protected void Repeater_admin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal litPower = (Literal)e.Item.FindControl("litPower");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string tpower = drv["power"].ToString();
                if (tpower == "0")
                {
                    litPower.Text = "超级管理员";
                }
                else
                {
                    litPower.Text = curGroupAdmin.GetName(tpower);
                }
            }
        }
        #endregion


        private string GetBindUrl()
        {
            return "power=" + power + "&page";
        }


        //批量删除
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)Repeater_admin.Items[i].FindControl("hidid")).Value.Trim());
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    curAdminer.Delete(id);
                    nub += 1;
                }
            }
            Response.Redirect("../Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }

    }
}